#pragma once
#include<map>
#include<vector>
#include<string>
#include"MMySql.h"
#include<mutex>

class EventLoop;
class Socket;
class Acceptor;
class TcpConnection;
class ThreadPool;
class Buffer;

class Sever
{
private:
    /* data */
    //主reactor 只负责新用户的连接
    EventLoop* mainReactor;
    Acceptor *acceptor;

    // 负责tcp连接
    std::map<int,std::shared_ptr<TcpConnection>> connection;
    std::mutex connMutex;
    // 绑定用户账号和登陆时候的端口号
    std::map<std::string,int> clntLinkConnection;
    std::map<int,std::string> ConnectionLinkclnt;
    std::mutex clntLink;


    //服务监听用户是否在线

    std::vector<EventLoop*> subReactor;//每个线程都定义成为一个reactor循环
    ThreadPool *thread_subReactor;
    //工作线程
    ThreadPool *thread_work;

    MMySql *mysql;
    Sever(const Sever&) = delete;
    Sever &operator = (const Sever&) = delete;

public:
    void init(EventLoop*);

    Sever(/* args */);
    Sever(EventLoop*);

    // const std::map<int,std::shared_ptr<TcpConnection>> &returnLiunk();

    static Sever& getinstance();
    ~Sever();
    //新的连接
    void newConnection(Socket *seve1_sock);
    // 断开连接
    void deletConnection(Socket * clnt_sock);

    ThreadPool* getThreadWork();

    //添加/删除连接到服务器的客户端
    void addConnection(int fd, std::shared_ptr<TcpConnection>conn);
    void remConnection(int fd);
    //添加/删除已经登陆的客户端
    void addClntLinkConnection(std::string,int fd);
    void remClntLinkConnection(int fd);

    std::map<int,std::shared_ptr<TcpConnection>> getConnection();
    std::map<std::string,int> getClntLinkConnection();
    std::map<int,std::string>  getConnectionLinkclnt();
};


